package com.neu.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.neu.dao.UsersDao;
import com.neu.entity.Goods;
import com.neu.entity.Users;
import com.neu.util.JDBCUtil;

public class UsersDaoImpl implements UsersDao {
	/**
	 *    查看全部商品的信息
	 */
	@Override
	public List<Goods> selectGoods() throws SQLException {
		
		return null;
	}
	/**
	 *    用户修改的个人信息
	 */
	@Override
	public boolean updateUsers(Users us) throws SQLException {
		boolean falg = false;
		Connection conn=null;
		try {
			conn = JDBCUtil.getConn();
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql = "select*from users where id=?";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, us.getId());
		int a = ps.executeUpdate();
		if(a>0) {
			System.out.println("修改成功");
		}
		return falg;
	}
	/**
	 *    查看全部的用户的信息
	 */
	@Override
	public List<Users> selectUsers() throws SQLException {
		
		Connection conn =null;
		try {
			conn=JDBCUtil.getConn();
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql = "select * from users ";
		PreparedStatement ps = conn.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		List<Users> list =new ArrayList<>();
		while(rs.next()) {
			list.add(new Users(rs.getInt(1), rs.getString(2), rs.getString(3),rs.getString(4), rs.getString(5), rs.getString(6)));
		}
		return list;
	}
	/**
	 * 
	 *   查看单一的用户信息:
	 */
	@Override
	public Users selectDanyi(int id) throws SQLException {
		Connection conn = null;
		try {
			conn =JDBCUtil.getConn();
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql ="select*from users where id=?";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, id);
		ResultSet rs = ps.executeQuery();
		Users us =null;
		if(rs.next()) {
			us = new Users(rs.getInt(1), rs.getString(2), rs.getString(3),rs.getString(4), rs.getString(5), rs.getString(6));
		}
		return us;
	}
	/**
	 * 增加用户的信息
	 * 
	 */
	@Override
	public boolean insert(Users us) throws SQLException {
		Connection conn = null;
		boolean flag = false;
		try {
			conn = JDBCUtil.getConn();
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql = "insert into users value(null,?,?,?,?,?)";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, us.getName());
		ps.setString(2, us.getPsaavord());
		ps.setString(3, us.getNickname());
		ps.setString(4, us.getCountry());
		ps.setString(5, us.getSex());
		int a = ps.executeUpdate();
		if(a>0) {
			System.out.println("增加成功");
		}
		return flag;
	}
	/**
	 *   删除用户的信息
	 */
	@Override
	public boolean delete(Users us) throws SQLException {
		boolean flag = false;
		Connection conn = null;
		try {
			conn= JDBCUtil.getConn();
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql = "delete from users where id=?";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, us.getId());
		int a = ps.executeUpdate();
		if(a>0) {
			System.out.println("删除成功");
		}
		return flag;
	}

}
